import 'package:enterprise_v6_app/app/core/http/http.dart';
import 'package:enterprise_v6_app/app/core/utils/interface.dart';
import 'package:enterprise_v6_app/app/data/entities/double_control/get_risk_object_by_id_entity.dart';
import 'package:get/get.dart';

class MeRegionalOrganizationTreeController extends GetxController {
  /// 组织数据
  List<GetRiskObjectByIdObjectChildren> treeList = [];

  /// 节点
  GetRiskObjectByIdObjectChildren selectedEntity = GetRiskObjectByIdObjectChildren();

  /// 初始化数据列表
  Future<void> onInitData(String id) async {
    if (treeList.isEmpty) {
      // 请求接口
      final res = await HttpUtil().generateGetRequest<GetRiskObjectByIdEntity>(
        Interface.getRiskObjectById,
        queryParameters: {'id': id},
      ).execute();
      res.onSuccess((result) {
        if (result.data != null) {
          treeList = result.data?.objectChildren ?? [];
          update();
        }
      });
    }
  }

  /// 点击节点
  Future<void> clickOnNode(GetRiskObjectByIdObjectChildren treeEntity) async {
    treeEntity.isExpand = !treeEntity.isExpand!;
    selectedEntity = treeEntity;
    update();
  }
}
